<?php

abstract class baseTLUPimAction extends sfAction
{

    private $profileDocumentService;

    public function preExecute()
    {
        $sessionVariableManager = new DatabaseSessionManager();
        $sessionVariableManager->setSessionVariables(array(
            'orangehrm_user' => Auth::instance()->getLoggedInUserId(),
        ));
        $sessionVariableManager->registerVariables();
        $this->setOperationName(OrangeActionHelper::getActionDescriptor($this->getModuleName(), $this->getActionName()));


        /* For highlighting corresponding menu item */
        $request = $this->getRequest();
        $initialActionName = $request->getParameter('initialActionName', '');
//        $initialModuleName = $request->getParameter('initialModuleName', '');

        if (empty($initialActionName)) {
            $loggedInEmpNum = $this->getUser()->getEmployeeNumber();
            $empNumber = $request->getParameter('empNumber');

            if (!empty($loggedInEmpNum) && $loggedInEmpNum == $empNumber) {
                $request->setParameter('initialModuleName', 'pim');
                $request->setParameter('initialActionName', 'viewMyDetails');
            } else {
                $request->setParameter('initialModuleName', 'pim');
                $request->setParameter('initialActionName', 'viewEmployeeList');
            }
        }

    }

    public function getDataGroupPermissions($dataGroups, $empNumber)
    {
        $loggedInEmpNum = $this->getUser()->getEmployeeNumber();

        $entities = array('Employee' => $empNumber);
        $self = $empNumber == $loggedInEmpNum;

        return $this->getContext()->getUserRoleManager()->getDataGroupPermissions($dataGroups, array(), array(), $self, $entities);
    }

    /**
     * Get ProfileDocumentService
     * @returns ProfileDocumentService
     */
    public function getProfileDocumentService()
    {
        if (is_null($this->profileDocumentService)) {
            $this->profileDocumentService = new ProfileDocumentService();
            $this->profileDocumentService->setEmployeeDao(new EmployeeDao());
        }
        return $this->profileDocumentService;
    }

    /**
     * Set ProfileDocumentService
     * @param ProfileDocumentService $profileDocumentService
     */
    public function setProfileDocumentService(ProfileDocumentService $profileDocumentService)
    {
        $this->profileDocumentService = $profileDocumentService;
    }

    protected function isSupervisor($loggedInEmpNum, $empNumber)
    {

        if (isset($_SESSION['isSupervisor']) && $_SESSION['isSupervisor']) {

            $empService = $this->getProfileDocumentService();
            $subordinates = $empService->getSubordinateList($loggedInEmpNum, true);

            foreach ($subordinates as $employee) {
                if ($employee->getEmpNumber() == $empNumber) {
                    return true;
                }
            }
        }
        return false;
    }

    protected function IsActionAccessible($empNumber)
    {

        $isValidUser = true;

        $loggedInEmpNum = $this->getUser()->getEmployeeNumber();

        $userRoleManager = $this->getContext()->getUserRoleManager();
        $accessible = $userRoleManager->isEntityAccessible('Employee', $empNumber);

        if ($empNumber != $loggedInEmpNum && (!$accessible)) {
            $isValidUser = false;
        }

        return $isValidUser;
    }

    protected function isAllowedAdminOnlyActions($loggedInEmpNumber, $empNumber)
    {

        if ($loggedInEmpNumber == $empNumber) {
            return false;
        }

        $userRoleManager = $this->getContext()->getUserRoleManager();
        $excludeRoles = array('Supervisor');

        $accessible = $userRoleManager->isEntityAccessible('Employee', $empNumber, null, $excludeRoles);

        if ($accessible) {
            return true;
        }

        return false;

    }

    protected function setOperationName($actionName)
    {
        $sessionVariableManager = new DatabaseSessionManager();
        $sessionVariableManager->setSessionVariables(array(
            'orangehrm_action_name' => $actionName,
        ));
        $sessionVariableManager->registerVariables();
    }

}